package com.eascs.p2p.admin.service.impl;

import javax.inject.Inject;

import com.alibaba.dubbo.config.annotation.Service;
import com.eascs.app.dao.PageResult;
import com.eascs.p2p.admin.manager.IInvestObjectManager;
import com.eascs.p2p.admin.manager.IInvestorIncomeDetailManager;
import com.eascs.p2p.admin.manager.ILoaneeRefundPlanManager;
import com.eascs.p2p.common.entity.LoaneeRefundPlanEntity;
import com.eascs.p2p.common.service.ILoaneeRefundPlanService;
import com.eascs.p2p.common.vo.LoaneeRefundPlanVO;
import com.eascs.p2p.common.vo.ModelResult;

@Service
public class LoaneeRefundPlanServiceImpl implements ILoaneeRefundPlanService {
	@Inject
	ILoaneeRefundPlanManager loaneeRefundPlanManager;
	@Inject
	IInvestObjectManager investObjectManager;
	@Inject
	IInvestorIncomeDetailManager investorIncomeDetailManager;

	@Override
	public ModelResult<PageResult<Object[]>> findPageLoaneeRefundPlanEntity(LoaneeRefundPlanVO vo) {
		ModelResult<PageResult<Object[]>> result = new ModelResult<PageResult<Object[]>>();
		try {
			result.setModel(loaneeRefundPlanManager.findPageLoaneeRefundPlanEntity(vo));
		} catch (Exception e) {
			e.printStackTrace();
			result.withError("分页查询当前期还款信息：", e.getMessage());
		}
		return result;
	}

	@Override
	public ModelResult<PageResult<Object[]>> findAllPageLoaneeRefundPlanEntity(LoaneeRefundPlanVO vo) {

		ModelResult<PageResult<Object[]>> result = new ModelResult<PageResult<Object[]>>();
		try {
			result.setModel(loaneeRefundPlanManager.findAllPageLoaneeRefundPlanEntity(vo));
		} catch (Exception e) {
			e.printStackTrace();
			result.withError("分页查询当前标的的所有还款信息：", e.getMessage());
		}
		return result;
	}

	@Override
	public ModelResult<LoaneeRefundPlanVO> findNeedRefundMoney(LoaneeRefundPlanVO vo) {

		ModelResult<LoaneeRefundPlanVO> result = new ModelResult<LoaneeRefundPlanVO>();
		try {
			result.setModel(loaneeRefundPlanManager.findNeedRefundMoney(vo));
		} catch (Exception e) {
			e.printStackTrace();
			result.withError("查询已经还款总额和融资金额：", e.getMessage());
		}
		return result;
	}

	@Override
	public ModelResult<LoaneeRefundPlanVO> findOverMoneyandLoanMoeny(LoaneeRefundPlanVO vo) {

		ModelResult<LoaneeRefundPlanVO> result = new ModelResult<LoaneeRefundPlanVO>();
		try {
			result.setModel(loaneeRefundPlanManager.findOverMoneyandLoanMoeny(vo));
		} catch (Exception e) {
			e.printStackTrace();
			result.withError("查询已经还款总额和融资金额：", e.getMessage());
		}
		return result;
	}

	@Override
	public ModelResult<Integer> getNeedBidNum(LoaneeRefundPlanVO vo) {

		ModelResult<Integer> result = new ModelResult<Integer>();
		try {
			result.setModel(loaneeRefundPlanManager.getNeedBidNum(vo));
		} catch (Exception e) {
			e.printStackTrace();
			result.withError("查询还款中的标的数量：", e.getMessage());
		}
		return result;
	}

	@Override
	public ModelResult<LoaneeRefundPlanEntity> findLoaneeRefundPlanById(String id) {
		ModelResult<LoaneeRefundPlanEntity> result = new ModelResult<LoaneeRefundPlanEntity>();
		try {
			result.setModel(loaneeRefundPlanManager.findLoaneeRefundPlanById(id));
		} catch (Exception e) {
			e.printStackTrace();
			result.withError("根据id,查询还款计划信息：", e.getMessage());
		}
		return result;
	}

	@Override
	public ModelResult<Boolean> refundMoney(String id, int status) {
		ModelResult<Boolean> result = new ModelResult<Boolean>();
		LoaneeRefundPlanEntity entity = loaneeRefundPlanManager.findLoaneeRefundPlanById(id);
		boolean flag1 = false;
		boolean flag2 = false;
		boolean flag3 = false;
		if (entity.getIssue()==entity.getTotalIssues()) {
			// 1.修改标的表有关信息
			flag1 = investObjectManager.verifyBid(entity.getObjectId(), 6,"");
			if (!flag1) {
				result.setModel(false);
				result.withError("refundMoney", "修改标的信息表错误");
				return result;
			}
		
		}
		// 2.修改还款计划表有关信息
		flag2 = loaneeRefundPlanManager.updateLoaneeRefundPlan(id, status);
		if (!flag2) {
			result.setModel(false);
			result.withError("refundMoney", "修改还款计划表信息错误");
			return result;
		}
		//3.用户投资明细表
		
		flag3 = investorIncomeDetailManager.update(entity.getObjectId(),entity.getIssue());
		if (!flag3) {
			result.setModel(false);
			result.withError("refundMoney", "修改用户投资明细信息表错误");
			return result;
		}
	
		result.setModel(true);
		return result;
	}

}
